// pages/complete/complete.js
const sharePost = require('../../request/sharePost.js')
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    userName: '',
    userPhone: '',
    userAddress: '',
    user_type: '',
    invite: {},
    info: {},
    invite_name: '',
    invite_phone: '',
    sid: '',
    userId: '',
    openid: '',
    isShowMenu: true
  },
  //注册
  addUser: function(){
    if (!this.data.userName) {
      wx.showToast({
        title: '用户名不能为空',
        icon: 'none',
        duration: 2000
      })
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.userPhone))) {
      wx.showToast({
        title: '请填写正确手机号',
        icon: 'none',
        duration: 2000
      })
    } else if (!this.data.userAddress) {
      wx.showToast({
        title: '地址不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
        console.log('修改查看global', app.globalData.info)
      if (!app.globalData.info._id) {
          let user_phone = {
              "user_phone": this.data.userPhone
          }
          sharePost.checkUser(user_phone).then(res => {
              if (res.success === false) {
                  wx.showToast({
                      title: '手机号已经注册过',
                      icon: 'none',
                      duration: 2000
                  })
                  wx.navigateTo({
                      url: '../main/main',
                  })
              } else {
                  console.log('this.data.invite', this.data.invite_name, this.data.user_type === 0, this.data.user_type == 0)
                  let params
                  if (this.data.user_type == 0) {
                      params = {
                          "openid": app.globalData.info.openid,
                          "user_name": this.data.userName,
                          "user_phone": this.data.userPhone,
                          "user_address": this.data.userAddress,
                          "user_type": this.data.user_type,
                          "invite_name": this.data.invite_name,
                          "invite_phone": this.data.invite_phone,
                          "sid": this.data.sid
                      }
                  } else {
                      params = {
                          "openid": app.globalData.info.openid,
                          "user_name": this.data.userName,
                          "user_phone": this.data.userPhone,
                          "user_address": this.data.userAddress,
                          "user_type": this.data.user_type,
                          "invite_name": '',
                          "invite_phone": '',
                          "sid": ''
                      }
                  }
                  sharePost.addUser(params).then(res => {
                      console.log('请求的res', res)
                    app.globalData.user_type = res.data.user_type
                    app.globalData.info = res.data
                      wx.navigateTo({
                          url: '../my/my',
                      })
                  })
              }
          })
      } else {
        let params2 = {
          "uid": this.data.userId,
          "user_name": this.data.userName,
          "user_phone": this.data.userPhone,
          "user_address": this.data.userAddress
        }
          console.log('请求的res2', params2)
        sharePost.userUpdate(params2).then(res => {
          console.log('请求的res2', res)
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 2000
          })

          //重新获取用户基本信息
          sharePost.userInfoGet({
            "openid": app.globalData.info.openid
          }).then(res => {
            console.log('请求的res', res, app.globalData.info.openid)
            if (res.data.length === 0) {
            } else {
              //已注册
              app.globalData.info = res.data
                setTimeout(function () {
                    wx.redirectTo({
                        url: '../my/my',
                    })
                }.bind(this), 2500);
            }
          })
        })
      }
    }
  },
  fillName: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  fillTel: function (e) {
    this.setData({
      userPhone: e.detail.value
    })
  },
  fillAddress: function (e) {
    this.setData({
      userAddress: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options.invite_name', options, app.globalData, 'JSON.stringify(app.globalData.info) !== "{}"', app.globalData.info !== "{}", app.globalData.info !== {}, app.globalData.info != {})
    if (app.globalData.info._id) {
      console.log('修改进来的', app.globalData.info.user_type === 0, app.globalData.info.user_type == 0)
      wx.setNavigationBarTitle({
        title: '修改完善信息'
      })
        this.setData({
            userInfo: app.globalData.userInfo,
            isShowMenu: true
        })
      if (app.globalData.info.user_type == 0) {
        this.setData({
          invite_name: app.globalData.info.invite_name,
          invite_phone: app.globalData.info.invite_phone,
          sid: app.globalData.info.sid
        })
      }
      this.setData({
        userName: app.globalData.info.user_name,
        userPhone: app.globalData.info.user_phone,
        userAddress: app.globalData.info.user_address,
        userId: app.globalData.info._id
      })
    } else {
      wx.setNavigationBarTitle({
        title: '注册用户信息'
      })
      console.log('注册进来的', options.user_type === 0, options.user_type == 0)
      if (options.user_type == 0) {
        this.setData({
          invite_name: options.invite_name,
          invite_phone: options.invite_phone,
          sid: options.sid
        })
      }
      this.setData({
        userInfo: app.globalData.userInfo,
        info: app.globalData.info,
        openid: options.openid,
        user_type: options.user_type,
        isShowMenu: false
      })
    }
    console.log(this.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  //页面展示时，触发动画
  onShow: function () {
    const app = getApp()
    app.slideupshow(this, 'slide_up1', -100, 1)
    setTimeout(function () {
      app.slideupshow(this, 'slide_up2', -100, 1)
    }.bind(this), 200);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up3', -100, 1)
    }.bind(this), 400);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up4', -100, 1)
    }.bind(this), 600);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up5', -100, 1)
    }.bind(this), 800);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up6', -100, 1)
    }.bind(this), 1000);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up7', -100, 1)
    }.bind(this), 1200);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})
